import { homeFollowSearch } from "./homeFollowSearch"
import { homeLiveLiveSearch } from "./homeLiveLiveSearch"
import { hourTop } from "./hourTop/hourTop"
import { Pages } from "./to/pages/Pages"
import { router } from "./to/pages/routerVisitor"
import { currentPage } from "./to/pages/utils/consts"
import { toHomeFollow } from "./to/toHomeFollow"
import { toHomeLiveLive } from "./to/toHomeLive"
import { getCurrentPackageName } from "./utils/getCurrentPackageName"
import { pullDownToRefresh } from "./utils/pullDownToRefresh"
import { useSetting } from "./utils/useSetting"
import { useStorage } from "./utils/useStorage"

export function taskZb() {
    const setting = useSetting()
    let totalCount = setting.totalCount
    // 打开快手
    if (getCurrentPackageName() !== 'com.smile.gifmaker') {
        // 获取屏幕截图
        app.launchPackage("com.smile.gifmaker")
        events.emit('log', "成功打开快手")
        sleep(1000)
    }
    let bounds: AutoJs.Rect = {
        centerX: () => device.width / 2,
        top: device.height / 5,
        bottom: device.height / 5 * 4
    } as AutoJs.Rect;
    switch (setting.from) {
        case '直播':
            const homeLiveLiveCtx = toHomeLiveLive(10)
            if (homeLiveLiveCtx.get(currentPage) !== Pages.homeLiveLive) {
                events.emit('log', `程序错误02 ${router.currentPage()}`)
                throw new Error(`程序错误02`)
            }
            const listNode = homeLiveLiveCtx.get<AutoJs.UiObject>('listNode')
            if (listNode) {
                bounds = listNode.bounds()
            }
            events.emit('log', `${bounds.centerX()} ${bounds.top} ${bounds.bottom}`)
            pullDownToRefresh(bounds)
            sleep(3000)
            break;
        case '关注':
            const homeFollowCtx = toHomeFollow(10)
            if (homeFollowCtx.get(currentPage) !== Pages.homeFollow) {
                events.emit('log', `程序错误02 ${router.currentPage()}`)
                throw new Error(`程序错误02`)
            }
            const listNode2 = homeFollowCtx.get<AutoJs.UiObject>('listNode')
            if (listNode2) {
                bounds = listNode2.bounds()
            }
            pullDownToRefresh(bounds)
            sleep(3000)
            break;
    }
    let index = 0;
    const storage = useStorage()
    let start = storage.get(`start`, false)
    while (totalCount > 0 && start) {
        try {
            start = storage.get(`start`, false)
            switch (setting.from) {
                case '小时榜':
                    hourTop(start)
                    break;
                case "关注":
                    const t1 = threads.start(() => {
                        if (index >= 5 || homeFollowSearch(index)) {
                            pullDownToRefresh(bounds)
                            sleep(3000)
                            index = 0;
                        } else {
                            index += 1;
                        }
                    });
                    t1.waitFor()
                    t1.join();
                    break;
                case '直播':
                    const t = threads.start(() => {
                        if (index >= 5 || homeLiveLiveSearch(index)) {
                            pullDownToRefresh(bounds)
                            sleep(3000)
                            index = 0;
                        } else {
                            index += 1;
                        }
                    });
                    t.waitFor()
                    t.join();
                    break;
                case '活动':
                    break;
            }
        } catch (e) {
            events.emit('log', `error ${(e as any).toString()}`)
        } finally {
            totalCount--
            sleep(setting.refreshInterval * 1000)
        }
    }
    home();
}